import Request from 'utils/request';

import GoodsPaymentApi from './goodsPaymentApi';

import GoodsAuction from 'models/oneAuction/goodsAuction';
import GoodsAuctionDetail from 'models/oneAuction/goodsAuctionDetail';

export default class AuctionApi {
  static getAuctionList(pagination) {
    const apiName = 'api/wehcatshop/activity/oneYuanList';

    let postData = {
      limit: pagination.pageSize,
      page: pagination.currentPage
    };

    return Request.post(apiName, postData).then(res => {
      let auctionList = res.data;
      let auctions = [];
      for (let i = 0; i < auctionList.length; i++) {
        let auction = auctionList[i];
        auction = new GoodsAuction(auction);
        auctions.push(auction);
      }

      pagination.totalCount = res.rows;
      return auctions;
    });
  }

  static getAuctionDetail(id) {
    const apiName = 'api/wehcatshop/activity/oneYuanGoodsInfo';

    return Request.post(apiName, {
      goodsDefId: id
    }).then(res => {
      let systemTime = res.systemTime;
      let auctionData = res.data;
      auctionData = new GoodsAuctionDetail(auctionData, systemTime);
      return {
        auctionData,
        systemTime
      }
    });
  }

  static getConcernCount(id) {
    const apiName = 'api/wehcatshop/goodsBrowsingHistory';

    let postData = {
      goodsDefId: id,
      limit: 1,
      page: 1
    };

    return Request.post(apiName, postData).then(res => {
      return res.rows;
    });
  }

  static getAuctionRecord(auctionData) {
    const apiName = 'api/wehcatshop/activity/oneYuanGoodsLogList';
    let postData = {
      goodsDefId: auctionData.defId,
      goodsModelId: auctionData.modelId
    };

    let maxRecordId = auctionData.maxRecordId;
    if (maxRecordId !== null) {
      postData.activityLogId = maxRecordId;
    } else {
      postData.activityLogId = 0;
    }

    return Request.post(apiName, postData).then(res => {
      return res.data;
    });
  }

  static toJoinAuction(auction, lnglat) {
    let data = auction.getPayJoinPriceObject();
    return GoodsPaymentApi.payPrice(data, lnglat, 'YDDD');
  }

  static toPayAuctionGoods(auction, lnglat) {
    let data = auction.getPayObject();
    return GoodsPaymentApi.payActivityGoodsList(data, lnglat, 'YYPM');
  }

  static offerPrice(auction) {
    let apiName = 'api/wehcatshop/activity/oneYuanGoodsLogAdd';
    let postData = {
      goodsDefId: auction.defId,
      goodsModelId: auction.modelId,
      activityPrice: auction.buyPrice
    };
    return Request.post(apiName, postData).then(res => {
      return res.msg;
    });
  }
}
